from sqlalchemy.orm import Session
import models, schemas
from typing import List


# 获取一个作者
def get_author_by_username(db: Session, username: str):
    return db.query(models.Author).filter(models.Author.username == username).first()


# 创建一个作者
def create_author(db: Session, author: schemas.AuthorCreate):
    db_author = models.Author(**author.dict())
    db.add(db_author)
    db.commit()
    db.refresh(db_author)
    return db_author


# 获取所有作者信息
def get_all_author(db: Session):
    return db.query(models.Author).all()


# 查询
def get_publish_by_name(db: Session, name: str):
    return db.query(models.Publish).filter(models.Publish.name == name).first()


# 创建出版社
def create_publish(db: Session, publish: schemas.PublishCreate):
    db_publish = models.Publish(**publish.dict())
    db.add(db_publish)
    db.commit()
    db.refresh(db_publish)
    return db_publish


# 获取所有出版社信息
def get_all_publish(db: Session):
    return db.query(models.Publish).all()


def get_book_by_title(db: Session, title: str):
    return db.query(models.Book).filter(models.Book.title == title).first()


# 根据作者id,出版社id列表,书籍信息创建书籍
def create_book_by_author(db: Session, book: schemas.BookCreate, auth_id: int, publish_id_list: List[int]):
    db_book = models.Book(**book.dict(), auth_id=auth_id)
    publish_obj_list = [db.query(models.Publish).filter(models.Publish.id == i).first() for i in publish_id_list]
    db_book.book_to_publish = publish_obj_list
    db.add(db_book)
    db.commit()
    db.refresh(db_book)
    return db_book


def get_all_books(db: Session):
    books = db.query(models.Book).all()
    result = []
    for obj in books:
        result.append({
            "id": obj.id,
            "title": obj.title,
            "price": obj.price,
            "publish_date": obj.publish_date,
            "authors": obj.book_to_author,
            "publishers": obj.book_to_publish
        })

        return result
